package top.zhangjianyong.blog.utils;

import top.zhangjianyong.blog.record.UserAgentRecord;
import ua_parser.Client;
import ua_parser.Parser;

/**
 * UserAgentUtils 工具类
 * 基于 ua-parser-java 实现 User-Agent 字符串解析
 */
public class UserAgentUtils {
    private static final Parser parser = new Parser();

    /**
     * 解析 User-Agent 字符串，返回 UserAgentRecord record
     *
     * @param userAgent User-Agent 字符串
     * @return UserAgentRecord 解析结果
     */
    public static UserAgentRecord parse(String userAgent) {
        Client client = parser.parse(userAgent);
        // 由于 ua-parser-java 的 device 只包含 family 字段，brand/model 需兼容空值
        String browserName = client.userAgent.family;
        String browserVersion = client.userAgent.major != null ? client.userAgent.major : "";
        String osName = client.os.family;
        String osVersion = client.os.major != null ? client.os.major : "";
        String deviceType = client.device.family;
        // ua-parser-java 1.x 只支持 family 字段，brand/model 设为 "" 以兼容 record
        String deviceBrand = "";
        String deviceModel = "";
        return new UserAgentRecord(
            browserName,
            browserVersion,
            osName,
            osVersion,
            deviceType,
            deviceBrand,
            deviceModel
        );
    }
} 